package com.xiaocz.hblsubstitutedriving.activities.activity.dialog;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.common.widgets.load.LoadFactory;
import com.xiaocz.common.widgets.load.core.LoadManager;
import com.xiaocz.common.widgets.load.view.AbsView;
import com.xiaocz.common.widgets.recycler.RecyclerViewAdapter;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.adapter.PriceListAdapter;
import com.xiaocz.hblsubstitutedriving.base.NativeActivity;
import com.xiaocz.hblsubstitutedriving.model.ExpenseStandard;
import com.xiaocz.hblsubstitutedriving.model.OrderPrice;
import com.xiaocz.hblsubstitutedriving.model.UserInfo;
import com.xiaocz.hblsubstitutedriving.utils.Constants;
import com.xiaocz.hblsubstitutedriving.utils.ImageUtils;
import com.xiaocz.hblsubstitutedriving.widget.EmptyView;
import com.xiaocz.hblsubstitutedriving.widget.ErrorView;
import com.xiaocz.hblsubstitutedriving.widget.LoadingView;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Time:2019/11/7
 * Created by xiaocz
 * Describe 电子名片
 */
public class DialogCardActivity extends NativeActivity {

    @BindView(R.id.layout_base)
    LinearLayout layout;
    @BindView(R.id.iv_qr_code_pay)
    AppCompatImageView appCompatImageView;
    @BindView(R.id.recycler_view)
    public RecyclerView mRecyclerView;
    @BindView(R.id.tv_free_charge)
    TextView tv_free_charge;
    @BindView(R.id.tv_c_price)
    TextView tv_c_price;

    /**
     * DATA
     */
    private LoadManager mLoadManager;
    private PriceListAdapter priceListAdapter;

    public static void show(Context context) {
        Intent intent = new Intent(context, DialogCardActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.dialog_card;
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        mLoadManager = LoadFactory.getInstance().register(mRecyclerView, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        priceListAdapter = new PriceListAdapter(new RecyclerViewAdapter.AdapterListenerImpl<OrderPrice>() {
            @Override
            public void onItemClick(RecyclerViewAdapter.ViewHolder<OrderPrice> holder, OrderPrice data) {
                super.onItemClick(holder, data);
            }
        });
        mRecyclerView.setAdapter(priceListAdapter);
        UserInfo info = new Gson().fromJson(AccountManagers.getAccount(), UserInfo.class);
        if (info != null) {
            Bitmap mBitmap = CodeUtils.createImage(Common.Constant.API_H5.replace("http", "https") + "qrcodeorder/order/" + AccountManagers.getUserId(), 180, 180, BitmapFactory.decodeResource(getResources(), R.mipmap.logo));
            appCompatImageView.setImageBitmap(mBitmap);
        }
        if (Constants.getLocation() != null) {
            webExpensestandard(Constants.getLocation().getProvince(), Constants.getLocation().getCity());
        } else {
            mLoadManager.showSuccessView();
            App.getInstance().showMessage(getResources().getString(R.string.none_location));
        }
    }

    @OnClick({R.id.tv_down})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_down:
                tvDown.setVisibility(View.GONE);
                //使用当前系统的日期加以调整作为照片的名称
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat dataFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss", Locale.CHINA);
                // 定义Image图片的文件名
                String imageFileName = dataFormat.format(date) + ".png";
                ImageUtils.saveImage(imageFileName, layout, mContext);
                break;
        }
    }

    @BindView(R.id.tv_down)
    TextView tvDown;

    /**
     * 计价标准
     */
    private void webExpensestandard(String province_name, final String city_name) {
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("province_name", province_name);
        asyncHttpPostFormData.addFormData("city_name", city_name);
        okHttpClient.post(Common.Constant.API_IP + "api/Common/getCityPrice", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                mLoadManager.showStateView(ErrorView.class);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                Type type = new TypeToken<RspModel<ExpenseStandard>>() {
                }.getType();
                RspModel<ExpenseStandard> rspModel = getAnalysisBase(mLoadManager, response, type, "计价标准");
                if (rspModel == null) {
                    mLoadManager.showStateView(EmptyView.class);
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.getData() != null && !"[]".equals(rspModel.getData().getList().toString())) {
                            ExpenseStandard expenseStandard=rspModel.getData();
                            priceListAdapter.replace(expenseStandard.getList());
                            tv_free_charge.setText(String.format("%s分钟",expenseStandard.getFreewaittime()));
                            tv_c_price.setText(String.format("每分钟/%s元",expenseStandard.getWaitprice()));
                        } else {
                            mLoadManager.showStateView(EmptyView.class);
                        }
                        break;
                    default:
                        mLoadManager.showStateView(ErrorView.class);
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }
}
